package com.boot.main.config.configDemo;

import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// url拦截器
@Component
public class UrlInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("UrlInterceptor, preHanle");
        String url = request.getRequestURI();
        if(url.toLowerCase().indexOf("hacker") != -1){
            System.out.println("prevent hacker visit.");
            return false;
        }else{
            return true;
        }
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handle, @Nullable ModelAndView modelAndView) throws Exception{
        System.out.println("UrlInterceptor, postHandle");
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handle, @Nullable Exception ex) throws Exception{
        System.out.println("UrlInterceptor, afterCompletion");
    }
}
